#!/bin/bash
# Copyright (C) 2024-2025 KoudaiPC Computer Technologies Co., Ltd.
# script name: publish.sh
# author: david921518@qq.com

################
# global vars
GLB_WORK_DIR=
GLB_DONE_FILE=
GLB_DEB_PORT=
GLB_DEB_VER=
GLB_YD_VER=
GLB_ROOT_DEV=
GLB_ROOT_PART=
GLB_INSTALLED_PKGS=()
GLB_NUM_INSTALLED_PKGS=0
################

user=$(whoami)
if [ x"$user" == x ]; then
  user="$USER"
fi

# 定义日志格式-前缀
log_prefix="[publish]"
# 定义日志目录和文件名
if [ "$user" == "root" ]; then
  log_file_dir="/var/log/kpc/huangdi"
else
  log_file_dir=~/.local/log/kpc/huangdi
fi
log_file_path="$log_file_dir/publish.log"

log_kmsg_path="/dev/kmsg"
log_con_path="/dev/tty1"

function log_init()
{
  # 文件不存在则创建
  if [ ! -d "$log_file_dir" ]; then
    mkdir -p $log_file_dir
    chmod -R 755 $log_file_dir
  fi
  if [ ! -f "$log_file_path" ]; then
    touch $log_file_path
  fi
}

# 定义日志打印格式
function log()
{
  # 定义日志时间格式
  local curtime=$(date "+%Y-%m-%d %H:%M:%S")
  echo "$curtime $log_prefix $@"
  echo "$curtime $log_prefix $@" >> $log_file_path
  #sleep 0.1
  if [ "$user" == "root" ]; then
    echo "$log_prefix [kmsg] $@" > $log_kmsg_path
    sleep 0.1
    #echo "$curtime $log_prefix [con] $@" > $log_con_path
    #sleep 0.1
  fi
}

function log_from_file()
{
  tmpfile=$@
  if [ ! -f "$tmpfile" ]; then
    log "no file [$tmpfile] to log"
    return 1
  fi

  local n=0
  local -a lists
  while read line;
  do
    lists[$n]="$line"
    ((n++))
  done <$tmpfile

  # 定义日志时间格式
  local curtime=$(date "+%Y-%m-%d %H:%M:%S")

  #echo "$curtime $log_prefix $@"
  #echo "$curtime $log_prefix $@" >>$log_file_path
  local i=0
  for i in $( seq 0 $((${#lists[*]} - 1)) )
  do
    echo "$curtime $log_prefix ${lists[i]}"
    echo "$curtime $log_prefix ${lists[i]}" >>$log_file_path
  done

  if [ "$user" == "root" ]; then
    #echo "$log_prefix [kmsg] $@" >$log_kmsg_path
    for i in $( seq 0 $((${#lists[*]} - 1)) )
    do
      echo "$curtime $log_prefix ${lists[i]}" >$log_kmsg_path
      [ $(( i%10 )) -ne 9 ] || sleep 0.1
    done

    #sleep 0.1

    #echo "$curtime $log_prefix [con] $@" >$log_con_path
    #for i in $( seq 0 $((${#lists[*]} - 1)) )
    #do
    #  echo "$curtime $log_prefix ${lists[i]}" >$log_con_path
    #done

    #sleep 0.1
  fi
}

# get Work Dir
function get_work_dir()
{
  ################
  # /kpcos/distro/huangdi/publish.sh
  # /kpcos/distro/huangdi/tools/
  # /kpcos/distro/huangdi/releases/1.0/targets/x86/64/host/
  ################

  local real_path=$(realpath $0)
  GLB_WORK_DIR=$(dirname "$real_path")
  log "WORK DIR is [$GLB_WORK_DIR]"
}

# run prepre tasks
function run_prepare_tasks()
{
  local tools_dir=$GLB_WORK_DIR/tools
  local bi_file=$tools_dir/prepare-bootstrap-pkgs.sh
  local bs_file=$tools_dir/prepare-bootstrap-tgz.sh
  local up_file=$tools_dir/prepare-upgrade-tgz.sh
  local git_file=$tools_dir/prepare-git-files.sh
  local img_file=$tools_dir/prepare-image-files.sh

  log "run $bi_file"
  (sudo $bi_file)

  log "run $bs_file"
  (sudo $bs_file)

  log "run $up_file"
  (sudo $up_file)

  log "run $git_file"
  ($git_file)

  log "run $img_file"
  (sudo $img_file)
}

# start script
log_init
get_work_dir
cd "$GLB_WORK_DIR"
run_prepare_tasks

exit 0
